//
//  LZSearchRequestPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/23.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZSearchRequestPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "LZUserModel.h"
#import "MJExtension.h"

#import "LZVideoListModel.h"
#import "LZRestaurantListModel.h"

extern NSString *customer;

static NSString *const kSearchPromotionVideoListPageUrl = @"/clientsalesPromotion/searchListPage";    // 搜索--首页列表
static NSString *const kSearchRestaurantListPageUrl = @"/clientsellerUser/searchListPage";    // 搜索--餐厅列表

@implementation LZSearchRequestPresenter

/**
 搜索--首页列表
 
 @param page 页码
 @param limit 每页结果条数
 @param success .
 @param fail .
 */
- (void)searchPromotionVideoListWithPrameter:(NSDictionary *)dic andPage:(int)page andLimit:(int)limit onSuccess:(void (^)(NSArray *, int, int, NSTimeInterval))success onFail:(PresenterCommonFail)fail {
    NSString *urlStr = COMBINATION(customer,kSearchPromotionVideoListPageUrl);
    NSMutableDictionary * params ;
    if (dic) {
        params = [dic mutableCopy];
    }
    else {
        params = [NSMutableDictionary dictionary];
    }
    
    if ([LZUserModel Instance].userToken.length>0) {
        params[@"token"] = [LZUserModel Instance].userToken;
    }
    
    params[@"cpage"] = [NSString stringWithFormat:@"%d",page];
    params[@"limit"] = [NSString stringWithFormat:@"%d",limit];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            int totlePageTemp = [resultData[@"data"][@"totalPages"] intValue];
            int totleCountTemp = [resultData[@"data"][@"totalCount"] intValue];
            NSTimeInterval currentTimeTemp  = [resultData[@"timestamp"] longLongValue];
            NSArray *arrTemp = resultData[@"data"][@"list"];
            
            NSMutableArray *listArrTemp = [[NSMutableArray alloc] init];
            for (int i = 0; i < arrTemp.count; i ++) {
                NSDictionary *dicTemp = arrTemp[i];
                LZVideoListModel *m = [LZVideoListModel mj_objectWithKeyValues:dicTemp];
                [listArrTemp addObject:m];
            }
            
            if (success) {
                success(listArrTemp,totlePageTemp,totleCountTemp,currentTimeTemp);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];
}

/**
 搜索--餐厅列表
 
 @param page 页码
 @param limit 每页结果条数
 @param success .
 @param fail .
 */
- (void)searchRestaurantListWithPrameter:(NSDictionary *)dic andPage:(int)page andLimit:(int)limit onSuccess:(void (^)(NSArray *, NSTimeInterval))success onFail:(PresenterCommonFail)fail {
  
    NSString *urlStr = COMBINATION(customer,kSearchRestaurantListPageUrl);
    NSMutableDictionary * params ;
    if (dic) {
        params = [dic mutableCopy];
    }
    else {
        params = [NSMutableDictionary dictionary];
    }
    
    if ([LZUserModel Instance].userToken.length>0) {
        params[@"token"] = [LZUserModel Instance].userToken;
    }
    
    params[@"cpage"] = [NSString stringWithFormat:@"%d",page];
    params[@"limit"] = [NSString stringWithFormat:@"%d",limit];
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:params success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCodeCorrect:resultData fail:fail]) {
            NSMutableArray *arrData = [NSMutableArray array];
            NSArray *arrResult = [resultData[@"data"] objectForKey:@"list"];
            for (int i = 0; i<arrResult.count; i++) {
                LZRestaurantListModel *model = [LZRestaurantListModel mj_objectWithKeyValues:arrResult[i]];
                [model changeCuisineNameByCuisineID];
                [arrData addObject:model];
            }
            
            NSTimeInterval currentTimeTemp  = [resultData[@"timestamp"] longLongValue];

            if (success) {
                success(arrData,currentTimeTemp);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        SAFE_BLOCK_INVOKE(fail, error.localizedDescription);
    }];

}

@end
